#pragma once
#include <vector>
#include "afxmt.h"
#include "protocol.h" 

typedef trade_daying_itemex2 MC_HS ,pMC_HS;
class MCHSData
{
public:
	MCHSData();
	MCHSData(const MCHSData &data);
	virtual~MCHSData();

	bool GetHSDataStruct(MC_HS &);
	CString & GetRowString();
	bool SetRowString(CString csRowStr);
	bool SetRowString(LPCTSTR lpStr);
	void SetEmpty();
	void operator=( const MCHSData &data);
	//bool operator>(const MCHSData &data);

	class SignTime {
	public:
		SignTime(int nTime){m_nTime = nTime;};
		SignTime(){m_nTime = 0;};
		~SignTime(){};
		bool operator>(const SignTime &t){
			return m_nTime > t.m_nTime;
		};
	public:
		int m_nTime;
	}_signtime;

	class SignDate {
	public:
		SignDate(int nDate){m_nDate = nDate;};
		SignDate(){m_nDate = 0;}
		~SignDate(){};
		bool operator!=(const SignDate &d){
			return m_nDate != d.m_nDate;
		}
		bool operator>(const SignDate &d){
			return m_nDate > d.m_nDate;
		};
	public:
		int m_nDate;
	}_signdate;

	SignTime &Time();
	SignDate &Date();

private:
	CString m_hscsData;
};


typedef std::vector<MCHSData> DataSet;
class MCHSDataSet:public DataSet
{
public:
	MCHSDataSet(void){};
	virtual ~MCHSDataSet(void){};
	void SafeLock()
	{
		m_lock.Lock();
	}
	void SafeUnlock()
	{
		m_lock.Unlock();
	}
protected:
    CCriticalSection m_lock;
};

class Data_Locker{
public:
	Data_Locker( MCHSDataSet& Data):mData(Data){
		//mData = *pData;
		mData.SafeLock();
	};
	~Data_Locker(){
		mData.SafeUnlock();
	};
private:
	 MCHSDataSet& mData;
};
class Locker{
public:
	Locker( CCriticalSection& sec):mSec(sec){
		//mData = *pData;
		mSec.Lock();
	};
	~Locker(){
		mSec.Unlock();
	};
private:
	CCriticalSection &mSec;
};
class MCDataSign
{
	friend class MCDataSource;
	friend void _stdcall syncThreadProc(void *p);
public:
	MCDataSign(void);
	virtual ~MCDataSign(void);

	MCHSDataSet & GetDataSet(){return m_Set;};
	MCHSData GetNewerData();
	bool Create(LPCTSTR lpURL);
	int  GetDataCount();
	void SetSvrURL(LPCTSTR lpURL){m_url = lpURL;};
	void Close();
	int  Run();
private:
	MCHSDataSet m_Set;
	CString m_url;
	HANDLE m_hSynThd;
	volatile int _run;
};

typedef map<CString ,MCDataSign *>  SIGNS_LIST;
class MCDataSource
{
public:
	MCDataSource(){};
	virtual ~MCDataSource();
public:
	bool AddSign(MCDataSign *sign,CString strStockID); //exist return false;
	MCDataSign *FindSign(CString strStockID);
	MCDataSign *CreateSign(CString strStockID);
	void   RemoveSign(CString strStockID);
	void   RemoveSign(MCDataSign *pSign = NULL);
private:
	CCriticalSection m_lock;
	SIGNS_LIST m_signs;
};
 
extern MCDataSource  g_MC_HP_Sign_Source; //data source global unit